import { login } from '@/api/login'
import tools from '@/utils'
const oldState = { // 用于重置state数据
  hfbgToken: tools.storage.getToken()
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_hfbgToken (state, token) {
    state.hfbgToken = token
  }
}
const actions = {
  systemLoin ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      login(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          tools.storage.setToken(res.data.data.token)
          commit('set_hfbgToken', res.data.data.token)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  systemLogout ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      tools.storage.removeToken()
      commit('set_hfbgToken', '')
      resolve('退出成功')
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
